package com.kc.business.service.impl;

import com.kc.business.domain.BizMenu;
import com.kc.business.domain.BizRoleMenu;
import com.kc.business.domain.vo.BizMenuVo;
import com.kc.business.enums.MenuTypeEnum;
import com.kc.business.mapper.BizMenuMapper;
import com.kc.business.service.IBizMenuService;
import com.kc.business.service.IBizRoleMenuService;
import com.kc.common.constant.Constants;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * FileName: BizMenuServiceImpl
 * Author:   TP
 * Date:     2020-10-30 09:02
 * Description:
 */
@Service
public class BizMenuServiceImpl implements IBizMenuService {

    @Autowired
    private BizMenuMapper menuMapper;

    @Autowired
    private IBizRoleMenuService roleMenuService;

    @Override
    public List<BizMenu> getList(BizMenuVo vo) {
        return menuMapper.getList(vo);
    }

    @Override
    public BizMenu getById(Integer id) {
        if (id != null && id > 0) {
            return menuMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public Map<String, Object> getActivePersonalTreeMenus(Integer roleId) {
        Map<String, Object> resultMap = new HashMap<>();
        // 获取组菜单
        List<BizMenu> groupMenus = menuMapper.getByGroupType(MenuTypeEnum.GROUP.getType());
        // 获取非组菜单
        List<BizMenu> activeMenus = menuMapper.getActiveWithoutCommonMenus();
        if (CollectionUtils.isNotEmpty(activeMenus)) {
            if (CollectionUtils.isNotEmpty(groupMenus)) {
                List<Map<String, Object>> treeMenu = new ArrayList<>();
                for (BizMenu groupMenu : groupMenus) {
                    Map<String, Object> map = new HashMap<>();
                    List<BizMenu> bizMenus = new ArrayList<>();
                    for (BizMenu menu : activeMenus) {
                        if (menu.getParentMenuId() != null && menu.getParentMenuId().equals(groupMenu.getId())) {
                            bizMenus.add(menu);
                        }
                    }
                    if (CollectionUtils.isNotEmpty(bizMenus)) {
                        List<BizMenu> sortedMenus = bizMenus
                                .stream()
                                .distinct()
                                .sorted(Comparator.comparing(BizMenu::getMenuSort))
                                .collect(Collectors.toList());
                        map.put("children", sortedMenus);
                        map.put("menuName", groupMenu.getMenuName());
                        map.put("id", groupMenu.getId());
                        treeMenu.add(map);
                    }
                }
                resultMap.put("menus", treeMenu);
            }
        }
        if (roleId != null && roleId > 0) {
            BizRoleMenu bizRoleMenu = new BizRoleMenu();
            bizRoleMenu.setRoleId(roleId);
            List<BizRoleMenu> roleMenus = roleMenuService.select(bizRoleMenu);
            if (CollectionUtils.isNotEmpty(roleMenus)) {
                resultMap.put("checkedKeys", roleMenus.stream().map(BizRoleMenu::getMenuId).collect(Collectors.toList()));
            }
        }
        return resultMap;
    }

    @Override
    public BizMenu getByMenuNameOrAppFlagId(String menuName, String appFlagId) {
        if (StringUtils.isEmpty(menuName) && StringUtils.isEmpty(appFlagId)) {
            return null;
        }
        return menuMapper.getByMenuNameOrAppFlagId(menuName, appFlagId);
    }

    @Override
    public int add(BizMenu bizMenu) {
        return menuMapper.insertSelective(bizMenu);
    }

    @Override
    public BizMenu getByAppFlagId(String appFlagId) {
        if (StringUtils.isNotEmpty(appFlagId)) {
            return menuMapper.getByAppFlagId(appFlagId);
        }
        return null;
    }

    @Override
    public BizMenu getByMenuName(String menuName) {
        if (StringUtils.isNotEmpty(menuName)) {
            return menuMapper.getByMenuName(menuName);
        }
        return null;
    }

    @Override
    public int updateById(BizMenu bizMenu) {
        if (bizMenu != null && bizMenu.getId() > 0) {
            return menuMapper.updateByPrimaryKeySelective(bizMenu);
        }
        return 0;
    }

    @Override
    public AjaxResult deleteById(Integer id, String username) {
        if (id != null && id > 0) {
            BizMenu bizMenu = menuMapper.selectByPrimaryKey(id);
            if (bizMenu == null) {
                return AjaxResult.error("操作失败，未找到相关记录");
            }
            if (Constants.DELETE.equals(bizMenu.getIsDeleted())) {
                return AjaxResult.success();
            }
            if (MenuTypeEnum.GROUP.getType().equals(bizMenu.getMenuType())) {//删除组
                // 如果组下有在用子菜单，不允许删除
                List<BizMenu> childMenus = menuMapper.getActiveMenusByParentMenuId(bizMenu.getId());
                if (CollectionUtils.isNotEmpty(childMenus)) {
                    return AjaxResult.error("操作失败，此菜单组下存在使用中子菜单，不允许删除");
                }
            }
            if (menuMapper.deleteById(id, username) > 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，数据库更新失败");
        }
        return AjaxResult.error("操作失败，请指定要删除的数据项");
    }
}
